# TODO: 导入必要的库和模块

# TODO: 加载数字数据集

# TODO: 将数据集划分为训练集和测试集

# TODO: 初始化变量以存储最佳准确率，相应的k值和最佳knn模型

# TODO: 初始化一个列表以存储每个k值的准确率

# TODO: 尝试从1到40的k值，对于每个k值，训练knn模型，保存最佳准确率，k值和knn模型

# TODO: 将最佳KNN模型保存到二进制文件

# TODO: 打印最佳准确率和相应的k值
import numpy as np
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
import pickle
from tqdm import tqdm
import matplotlib.pyplot as plt

# 加载手写数字数据集
digits = load_digits()
X, y = digits.data, digits.target

# 将数据集划分为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# 初始化变量以存储最佳准确率，相应的k值和最佳knn模型
best_accuracy = 0
best_k = 0
best_model = None

# 初始化一个列表以存储每个k值的准确率
accuracies = []

# 尝试从1到40的k值，对于每个k值，训练knn模型，保存最佳准确率，k值和knn模型
for k in tqdm(range(1, 41)):
    model = KNeighborsClassifier(n_neighbors=k)
    model.fit(X_train, y_train)
    predictions = model.predict(X_test)
    acc = accuracy_score(y_test, predictions)
    accuracies.append(acc)
    
    if acc > best_accuracy:
        best_accuracy = acc
        best_k = k
        best_model = model

# 将最佳KNN模型保存到二进制文件
with open('best_knn_model.pkl', 'wb') as f:
    pickle.dump(best_model, f)

# 打印最佳准确率和相应的k值
print(f"Best accuracy: {best_accuracy * 100:.2f}% with k={best_k}")

# 绘制准确率变化图
plt.figure(figsize=(10, 6))
plt.plot(range(1, 41), accuracies, label='Accuracy')
plt.axvline(x=best_k, color='r', linestyle='--', linewidth=2, label=f'Best k={best_k}, Accuracy={best_accuracy * 100:.2f}%')
plt.text(best_k, accuracies[best_k-1], f'({best_k}, {best_accuracy * 100:.2f}%)', ha='right', va='bottom')
plt.title('Accuracy vs. K for KNN Model')
plt.xlabel('K Value')
plt.ylabel('Accuracy')
plt.legend()
plt.grid(True)
plt.savefig('accuracy_plot.pdf')
plt.show()




